package com.rgbgame.physics;

import org.jbox2d.collision.Shape;
import org.jbox2d.dynamics.ContactFilter;

/**
 * Slightly extends the default JBox2d ContactFilter so that
 * the Actor::shouldCollide method may override the usual
 * groupIndex/maskBits/categoryBits check.
 */
public class RGBContactFilter implements ContactFilter {

	/**
	 * Uses groupIndex, maskBits and categoryBits to determine whether to process
	 * collision unless the Actor::shouldCollide method has been overridden to prevent
	 * collisions.
	 */
	public boolean shouldCollide(Shape shape1, Shape shape2) {
		
		Actor actor1 = (Actor)(shape1.getBody().getUserData());
		Actor actor2 = (Actor)(shape2.getBody().getUserData());
		if (!actor1.shouldCollide(actor2)) return false;
		
		if (shape1.m_groupIndex == shape2.m_groupIndex && shape1.m_groupIndex != 0) {
			return shape1.m_groupIndex > 0;
		}

		boolean collide = (shape1.m_maskBits & shape2.m_categoryBits) != 0 && (shape1.m_categoryBits & shape2.m_maskBits) != 0;
		return collide;
	}

}
